package com.weijie.relaxed.activity;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.weijie.relaxed.App;
import com.weijie.relaxed.R;
import com.weijie.relaxed.fragment.ContactListFragment;
import com.weijie.relaxed.fragment.ConversationFragment;
import com.weijie.relaxed.fragment.SettingsFragment;
import com.weijie.relaxed.manager.UserManager;
import com.weijie.relaxed.net.Api;
import com.weijie.relaxed.net.StringListener;
import com.weijie.relaxed.views.TabButton;

public class MainActivity extends FragmentActivity implements OnClickListener {

	TabButton conversation,contact_list,settings;

	Fragment conversationF,contactF,settingsF;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		conversation = (TabButton)findViewById(R.id.conversation);
		contact_list = (TabButton)findViewById(R.id.contact_list);
		settings = (TabButton)findViewById(R.id.settings);
		conversation.setSelected(true);
		conversation.setOnClickListener(this);
		contact_list.setOnClickListener(this);
		settings.setOnClickListener(this);

		conversationF = new ConversationFragment();
		getSupportFragmentManager().beginTransaction().add(R.id.content, conversationF).commit();

		SharedPreferences share = getSharedPreferences("token", Context.MODE_PRIVATE);
		String access_token = share.getString("access_token", "");
		long expires_in = share.getLong("expires_in", 0);
		if(expires_in<new Date().getTime() || access_token.equals("")) {
			Api.getToken(App.getInstance(getApplication()), new Listener());
		} else {
			Api.getFriendList(App.getInstance(getApplication()), new FriendListener(), access_token, UserManager.getInstance(this).getName());
		}

	}

	class Listener extends StringListener {

		@Override
		public void onResponse(String response) {
			try {
				JSONObject js = new JSONObject(response);
				Editor edit = getSharedPreferences("token", Context.MODE_PRIVATE).edit();
				edit.putString("access_token", js.optString("access_token"));
				edit.putString("application", js.optString("application"));
				edit.putLong("expires_in", new Date().getTime()+js.optLong("expires_in"));
				edit.commit();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
		}

	}

	class FriendListener extends StringListener {

		@Override
		public void onResponse(String response) {
			//Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();

		}

		@Override
		public void onErrorResponse(VolleyError error) {
			Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.conversation:
			if(!conversation.isSelected()) {
				conversation.setSelected(true);
				contact_list.setSelected(false);
				settings.setSelected(false);

				if(conversationF==null) {
					conversationF = new ConversationFragment();
					setFragemnt(conversationF,true);
				} else {
					setFragemnt(conversationF,false);
				}
			}

			break;

		case R.id.contact_list:

			if(!contact_list.isSelected()) {
				contact_list.setSelected(true);
				conversation.setSelected(false);
				settings.setSelected(false);

				if(contactF==null) {
					contactF = new ContactListFragment();
					setFragemnt(contactF,true);
				} else {
					setFragemnt(contactF,false);
				}
			}

			break;
		case R.id.settings:

			if(!settings.isSelected()) {
				settings.setSelected(true);
				contact_list.setSelected(false);
				conversation.setSelected(false);

				if(settingsF==null) {
					settingsF = new SettingsFragment();
					setFragemnt(settingsF,true);
				} else {
					setFragemnt(settingsF,false);
				}
			}
			break;
		default:
			break;
		}
	}

	/**
	 * b为true的时候用add,false的时候用show
	 * @param fragment
	 * @param b
	 */
	private void setFragemnt(Fragment fragment,boolean b) {
		if(fragment==conversationF) {
			if(contactF!=null) {
				getSupportFragmentManager().beginTransaction().hide(contactF).commit();
			}
			if(settingsF!=null) {
				getSupportFragmentManager().beginTransaction().hide(settingsF).commit();
			}
			if(!b) {
				getSupportFragmentManager().beginTransaction().show(conversationF).commit();
			} else {
				getSupportFragmentManager().beginTransaction().add(R.id.content, conversationF).commit();
			}
		} else if(fragment==contactF) {
			if(conversationF!=null) {
				getSupportFragmentManager().beginTransaction().hide(conversationF).commit();
			}
			if(settingsF!=null) {
				getSupportFragmentManager().beginTransaction().hide(settingsF).commit();
			}
			if(!b) {
				getSupportFragmentManager().beginTransaction().show(contactF).commit();
			} else {
				getSupportFragmentManager().beginTransaction().add(R.id.content, contactF).commit();
			}
		} else {
			if(conversationF!=null) {
				getSupportFragmentManager().beginTransaction().hide(conversationF).commit();
			}
			if(contactF!=null) {
				getSupportFragmentManager().beginTransaction().hide(contactF).commit();
			}
			if(!b) {
				getSupportFragmentManager().beginTransaction().show(settingsF).commit();
			} else {
				getSupportFragmentManager().beginTransaction().add(R.id.content, settingsF).commit();
			}
		}
	}

	@Override
	protected void onDestroy() {
		/*EMChatManager.getInstance().logout(new EMCallBack(){
			@Override
			public void onError(int arg0, String arg1) {
			}

			@Override
			public void onProgress(int arg0, String arg1) {
			}

			@Override
			public void onSuccess() {
			}});*/
		super.onDestroy();
	}

}
